package com.wu.advapplication.utils;


import android.content.Context;
import android.os.Handler;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Like AnalogClock, but digital. Shows seconds.
 */
public class DateTextView extends TextView {
    private Context context;
    private Runnable mTicker;
    private Handler mHandler;

    public DateTextView(Context context) {
        super(context);
        this.context = context;

    }

    public DateTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mHandler = new Handler();

        /**
         * requests a tick on the next hard-second boundary
         */
        mTicker = new Runnable() {
            public void run() {
                setText(TimeUtils.getCurrentTimeInString(TimeUtils.DATE_FORMAT_DATE) + TimeUtils.getWeek(context, null));
                long now = SystemClock.uptimeMillis();
                long next = now + (10000 - now % 10000);
                mHandler.postAtTime(mTicker, next);
            }
        };
        mTicker.run();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

}
